package Dao;

import java.lang.reflect.InvocationTargetException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import javax.swing.JOptionPane;


import entity.Intnum;
import entity.Longnum;
import entity.User;
import entity.Word;
import util.Dbconnect;

public class UserDaoimpl implements UserDao{

	/**
	* Title: findByid
	* Description: 用email 查询用户
	* @author: 刘嘉龙
	* @param email
	* @return
	* @throws Exception
	 */
	@Override
	public User findByemail(String email) throws Exception {
		String sql="select * from user where email=?";
		PreparedStatement st = Dbconnect.init(sql);
		st.setString(1,email);
		User us = Dbconnect.queryone(st, User.class);
		Dbconnect.close(st);
		return us;
	}
	
	/**
	* Title: add
	* Description: 注册
	* @author: 刘嘉龙
	* @param u
	* @return
	* @throws Exception
	 */
	@Override
	public Integer add(User u) throws Exception {
		
		String sql="select * from user where email=?";
		PreparedStatement st = Dbconnect.init(sql);
		st.setString(1,u.getEmail());
		User user = Dbconnect.queryone(st, User.class);
		if(user != null) {
			return -1; 
		}else {
			sql="insert into user(email,sex,password,typeid,name) values(?,?,?,?,?)";
			PreparedStatement st1 = Dbconnect.init(sql);
			st1.setString(1,u.getEmail());
			st1.setString(2,u.getSex());
			st1.setString(3,u.getPassword());
			st1.setInt(4,u.getTypeid());
			st1.setString(5,u.getName());
			Dbconnect.update(st1);
			Dbconnect.close(st1);
		}
		Dbconnect.close(st);
		return 0;
	}
	

	/**
	* Title: update
	* Description: 更新用户信息
	* @author: 刘嘉龙
	* @param u
	* @return
	* @throws Exception
	 */
	@Override
	public boolean update(User u) throws Exception {
		// TODO Auto-generated method stub
		String sql="update user set email=?,name = ?,sex=?, password=?,typeid=? where email =?";
		PreparedStatement st = Dbconnect.init(sql);
		st.setString(1,u.getEmail());
		st.setString(2,u.getName());
		st.setString(3,u.getSex());
		st.setString(4,u.getPassword());
		st.setString(5,u.getPassword());
		st.setString(6,u.getEmail());
		int update = Dbconnect.update(st);
		Dbconnect.close(st);
		return update>0;
	}

	
    /**
    * Title: getUser
    * Description: 获取单个用户
    * @author: 刘嘉龙
    * @param email
    * @param password
    * @return User
    * @throws Exception
     */
	@Override
	public User getUser(String email, String password) throws Exception {
		// TODO Auto-generated method stub
		String sql="select * from user where email= ? and password = ?";
		PreparedStatement st = Dbconnect.init(sql);
		st.setString(1,email);
		st.setString(2,password);
		User user = Dbconnect.queryone(st, User.class);
		Dbconnect.close(st);
		return user;
	}
	
	/**
	 * 
	* Title: getUserList
	* Description: 获取用户列表
	* @author: 刘嘉龙
	* @return
	* @throws Exception
	 */
	@Override
	public List<User> getUserList() throws Exception {
		// TODO Auto-generated method stub
		String sql="select * from user";
		PreparedStatement st = Dbconnect.init(sql);
		List<User> query = Dbconnect.query(st, User.class);
		Dbconnect.close(st);
		return query;
	}
	
	@Override
	public void remove(String userid) throws SQLException{
		
	}
    /**
	* 
	* Title: updateGrade
	* Description: 提升等级
	* @author: 刘嘉龙
	* @throws Exception
	 */
	@Override
	public void updateGrade(String email) throws Exception {
		String sql = "select count(*) num from record where email= ?";
		PreparedStatement st = Dbconnect.init(sql);
		st.setString(1,email);
		Long num = Dbconnect.queryone(st,Longnum.class).getNum();
		String sql1 ="update user set grade= ? where email= ? ";
		PreparedStatement st1 = Dbconnect.init(sql1);
		st1.setLong (1,num/User.ruler);
		st1.setString(2,email);
		Dbconnect.update(st1);
		Dbconnect.close(st1);
		Dbconnect.close(st);

	}
	public static void main(String[] args) {
		UserDaoimpl u1 = new UserDaoimpl();
		try {
			u1.updateGrade("2263130346@qq.com");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}



}
